package com.rongma.waterdrop.manager.bmobmanager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.rongma.waterdrop.db.WaterDropHelper;
import com.rongma.waterdrop.global.WaterDropApplication;
import com.rongma.waterdrop.model.DefaultNews;

import java.util.ArrayList;

/**
 * Created by RongMa on 16/6/2.
 */
public class DefaultNewsManager
{
    private static final String DB_NAME = "defaultnews";
    private static final String COLUMN_TYPE = "type";
    private static final String COLUMN_CONTENT = "content";
    private static final String COLUMN_SCORE = "score";

    private static final DefaultNewsManager manager = new DefaultNewsManager();

    private static WaterDropHelper waterDropHelper;

    private DefaultNewsManager()
    {

    }

    public static DefaultNewsManager getManager(Context context)
    {
        waterDropHelper = new WaterDropHelper(context);
        return manager;
    }

    //添加数据到数据库
    public void saveDefaultNews(DefaultNews news)
    {

        if(news != null)
        {
            SQLiteDatabase sdb = waterDropHelper.getWritableDatabase();
            sdb.beginTransaction();
            try
            {
                ContentValues values = new ContentValues();
                values.put(COLUMN_TYPE,news.getType());
                values.put(COLUMN_CONTENT,news.getContent());
                values.put(COLUMN_SCORE,news.getScore());
                sdb.insert(DB_NAME,null,values);
                sdb.setTransactionSuccessful();
            }
            finally
            {
                sdb.endTransaction();
                sdb.close();
            }
        }
    }

    //添加批量数据到数据库
    public void saveDefaultNewsList(ArrayList<DefaultNews> defaultNewsList)
    {
        if(defaultNewsList != null && defaultNewsList.size() > 0)
        {
            SQLiteDatabase sdb = waterDropHelper.getWritableDatabase();
            sdb.beginTransaction();
            try
            {
                for(DefaultNews news : defaultNewsList)
                {
                    if(news != null)
                    {
                        ContentValues values = new ContentValues();
                        values.put(COLUMN_TYPE, news.getType());
                        values.put(COLUMN_CONTENT, news.getContent());
                        values.put(COLUMN_SCORE, news.getScore());
                        sdb.insert(DB_NAME, null, values);
                    }
                }
                sdb.setTransactionSuccessful();
            }
            finally
            {
                sdb.endTransaction();
                sdb.close();
            }
        }
    }

    //查询所有的DefaultNews
    public ArrayList<DefaultNews> queryDefaultNews()
    {
        ArrayList<DefaultNews> defaultNewsList = new ArrayList<DefaultNews>();
        SQLiteDatabase sdb = waterDropHelper.getReadableDatabase();
        Cursor cursor = sdb.query(DB_NAME,null,null,null,null,null,null);
        try
        {
            if(cursor.moveToFirst()) {
                do {
                    DefaultNews defaultNews = new DefaultNews();
                    defaultNews.setType(cursor.getInt(cursor.getColumnIndex(COLUMN_TYPE)));
                    defaultNews.setContent(cursor.getString(cursor.getColumnIndex(COLUMN_CONTENT)));
                    defaultNews.setScore(cursor.getLong(cursor.getColumnIndex(COLUMN_SCORE)));
                    defaultNewsList.add(defaultNews);
                } while (cursor.moveToNext());
            }
        }
        finally
        {
            cursor.close();
            sdb.close();
        }
        return defaultNewsList;
    }



}
